package com.example.job;

import com.example.domain.entity.Orders;
import com.example.domain.entity.Room;
import com.example.mapper.OrdersMapper;
import com.example.mapper.RoomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class UpdateStatus {

    @Autowired
    private OrdersMapper orderMapper;

    @Autowired
    private RoomMapper roomMapper;

    @Scheduled(fixedRate = 5000)
    public void updateOrderStatus() {
        // Update orders status to 4 where applicable
        List<Orders> ordersList = orderMapper.selectList(null);
        ordersList = ordersList.stream().map(orders -> {
            if (orders.getPaymentDeadline().isBefore(LocalDateTime.now())&&orders.getStatus()==1) {
                orders.setStatus(4);
                orderMapper.updateById(orders);
                Room room = new Room();
                room.setId(orders.getRoomId());
                room.setStatus(0);
                roomMapper.updateById(room);
            }
            return orders;
        }).collect(Collectors.toList());

    }
}